<template>
  <div class="user-info">
    <el-row :span="24">
      <el-col :span="8">
        <basic-container>
          <div class="user-info__content">
            <img class="user-info__img" :src="form.img" alt />
            <p class="user-info__name">{{ form.usrName }}</p>
            <router-link
              class="user-info__setting"
              :to="{ path: '/info/setting' }"
              >个人设置</router-link
            >
            <p class="user-info__desc">{{ form.note }}</p>
            <div class="user-info__detail-desc">
              <p>
                <i class="el-icon-user"></i>
                <span>{{ form.realName }}</span>
              </p>
              <p>
                <i class="el-icon-phone"></i>
                <span>{{ form.mobile }}</span>
              </p>
              <p>
                <i class="el-icon-message"></i>
                <span>{{ form.email }}</span>
              </p>
              <p>
                <i class="el-icon-postcard"></i>
                <span>{{ form.dept_names }}</span>
              </p>
            </div>
            <div class="user-info__divider"></div>
            <h4>标签</h4>
            <div class="user-info__tags">
              <el-tag
                effect="plain"
                v-for="(tag, index) in tags"
                :key="index"
                >{{ tag }}</el-tag
              >
            </div>
          </div>
        </basic-container>
      </el-col>
      <el-col :span="16">
        <basic-container>
          <avue-tabs
            :option="option"
            v-model="form"
            @chang="handleChange"
            @submit="handleSubmit"
          ></avue-tabs>
        </basic-container>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import option from "@/const/user/info";
import { baseUrl } from "@/config/env";
import { resetRouter } from "@/router/router";
const url = {
  changePwd: baseUrl + "/cp/changePwd",
  reset: baseUrl + "/cp/resetPwd",
  user: baseUrl + "/cp/api/getUserInfo",
  dept_list: baseUrl + "/cp/dept/au-dept/tree",
  dept_name: baseUrl + "/cp/usr/au-usr-dept/deptlist",
  dept_my_list: baseUrl + "/cp/usr/au-usr-dept/list",
  updateuser: baseUrl + "/cp/api/updateUserInfo"
};
export default {
  data() {
    return {
      type: "info",
      option: option,
      tags: [
        " 善解人意",
        "开朗乐观",
        "真诚热情",
        "心地善良",
        "谦恭有礼",
        "彬彬有礼",
        "虚怀若谷",
        "严于律己",
        "雍容大度",
        "热情洋溢",
        "从容自若",
        "诚挚",
        "温厚",
        "谦让",
        "勤恳",
        "耿直"
      ],
      form: {
        img: "/img/logo_s.png",
        username: "smallwei",
        name: "smallwei",
        ms: "保护头发，人人有责",
        yx: "1634566606@qq.com",
        gs: "某某公司",
        bm: "某某部门",
        zw: "前端开发工程师",
        dz: "内蒙古",
        bq: [1, 2, 3, 4],
        mobile: null,
        oldpassword: "",
        newpassword: "",
        newpasswords: ""
      },
      deptData: [],
      defaultCheckedKeys: [],
      defaultProps: {
        children: "children",
        label: "name"
      },
      popoverHide(checkedIds, checkedData) {
        this.form.depts = checkedIds.toString();
      }
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    handleSubmit(e) {
      var _this = this;
      console.log(e);
      if (!e.newpassword && !e.newpasswords && !e.oldpassword) {
        axios
          .post(url.updateuser, {
            objectId: this.form.objectId,
            email: this.form.email,
            realName: this.form.realName,
            mobile: this.form.mobile,
            note: this.form.note
          })
          .then(res => {
            this.$message({
              message: res.data.result,
              type: "success",
              duration: 1000,
              onClose: function() {}
            });
          });
      } else if (!e.oldpassword) {
        this.$message({
          message: "请输入原密码",
          type: "warning"
        });
        return false;
      } else if (!e.newpassword || !e.newpasswords) {
        this.$message({
          message: "请输入新密码",
          type: "warning"
        });
        return false;
      } else {
        axios
          .post(url.changePwd, {
            oldPassword: this.form.oldpassword,
            newPassword1: this.form.newpassword,
            newPassword2: this.form.newpasswords
          })
          .then(res => {
            this.$message({
              message: "密码修改成功",
              type: "success",
              duration: 1000,
              onClose: function() {
                _this.$store.dispatch("LogOut").then(() => {
                  resetRouter();
                  _this.$router.push({ path: "/login" });
                });
              }
            });
          });
      }
    },
    handleChange(item) {
      this.type = item.prop;
    },
    updateselect(data, node, done) {
      console.log(data);
    },
    addselect(data, node, done) {
      console.log(data);
    },
    fetch() {
      axios.get(url.user).then(res => {
        this.form = res.data.result;
        axios
          .get(url.dept_name, { params: { usrId: this.form.objectId } })
          .then(res => {
            const dept_list = res.data.result;
            const dept_name = dept_list.map(item => {
              return item.NAME;
            });
            this.form.dept_names = dept_name.join(",");
          });
      });
    },
    reset() {
      // axios.get(url.reset).then(() => {
      //   this.$message({
      //     message : "密码重置成功，密码为123456",
      //     type : "success"
      //   })
      // })
    },
    mounted() {
      console.log(this.option);
    }
  }
};
</script>

<style lang="scss">
.user-info {
  .avue-tabs {
    padding: 0 10px;
  }
  .el-tabs__content {
    padding: 20px 0;
  }
  &__img {
    display: block;
    margin: 0 auto;
    border-radius: 100%;
    width: 100px;
    height: 100px;
  }
  &__name {
    text-align: center;
    font-size: 20px;
    line-height: 28px;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.85);
    margin-bottom: 0;
    margin-top: 10px;
  }
  &__setting {
    margin-bottom: 12px;
    display: block;
    font-size: 12px;
    color: #409eff;
    text-align: center;
  }
  &__desc {
    text-align: center;
    width: 200px;
    margin: 0 auto;
  }
  &__detail-desc {
    margin-top: 50px;
    font-size: 14px;
    p {
      margin-bottom: 10px;
    }
    span {
      margin-left: 10px;
    }
  }
  &__divider {
    border-top: 1px dashed #e8e8e8;
    display: block;
    height: 0;
    width: 100%;
    margin: 24px 0;
    clear: both;
  }
  &__tags {
    .el-tag {
      margin: 0 5px 5px 0;
    }
  }
}
</style>
